<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商品回购</title>
<link href="${basePath }/css/ui.css" rel="stylesheet" type="text/css">
<jsp:include page="/header.jsp"></jsp:include>
<script type="text/javascript">
	$(function() {
		$(window).keydown(function(event){
			switch(event.keyCode) {
				case 13:scan();
			}
		});
		
		resetSum();
	});
	
	function scan() {
		var tds = $("#list_div").find("tr").find("td:eq(2)");
		var codes = tds.map(function(){
			return $(this).text();
		}).get().join(",");
		
		var code = $("#code").val();
		if(code==""||code==null) $.jqalert("要扫描的二维码不能为空！！","提示");
		else if(codes.indexOf(code)!=-1) $.jqalert("该二维码已经扫过！！","提示");
		else {
			var tr = $("<tr></tr>");
			tr.load("${basePath}/buyback/scan.htm?code="+code, function() {
				if($.trim(tr.html())!="") {
					$("#list_div>div>table>tbody").append(tr);
					resetSum();
				}
				else $.jqalert("查询不到对应商品！！", "提示");
			});
		}
	}
	
	function delone(a) {
		$(a).parent().parent().remove();
		resetSum();
	}
	
	function clearAll() {
		$("#list_div>div>table").find("tr:gt(0)").remove();
		resetSum();
	}
	
	function selectchange(select) {
		var select = $(select);
		var discount = Number(select.val());
		var new_price = Number(select.parent().prev().text());
		select.parent().next().text(new_price.mul(discount));
	}
	
	function buyconfirm() {
		var customerId = $("#final_customer_table").find("input[name='id']").val();
		var productnum = $("#list_div").find("table").find("tr").size();
		if(customerId==""||customerId==null) $.jqalert("请录入客户信息！！", "提示");
		else if(productnum==1) $.jqalert("请选择要回购的商品！！", "提示");
		else {
			var products = new Array();
			var backType = $("input[name='back_type']").val();
			var trs = $("#list_div").find("tr:gt(0)");
			$.each(trs, function(i,n){
				var product = {};
				$.each($(n).find("td"), function(j, d) {
					var value = "";
					if(j==8) value = $(d).find("select").val();
					else value = $(d).text();
					eval("product."+$(d).attr("title")+"=\""+value+"\"");
				});
				product.product_id = $(n).find("input[type='hidden']").val();
				products.push(product);
			});
			
			var customer = {};
			$.each($("#final_customer_table").find("span[class!='info_s fl']"), function(i, n){
				eval("customer."+$(n).attr("name")+"=\""+$(n).text()+"\"");
			}); 
			customer.id = $("#final_customer_table").find("input[name='id']").val();
			var form = $("<form action='${basePath}/buyback/final.htm' method='post'></form>");
			var hidden1 = $("<input type='hidden' name='backType' value='"+backType+"'>");
			var hidden2 = $("<input type='hidden' name='products' value='"+JSON.stringify(products)+"'>");
			var hidden3 = $("<input type='hidden' name='customer' value='"+JSON.stringify(customer)+"'>");
			var sumprice = $("#sum_price").text();
			var hidden4 = $("<input type='hidden' name='sumprice' value='"+sumprice+"'>");
			form.append(hidden1);form.append(hidden2);form.append(hidden3);form.append(hidden4);
			form.submit();
		}
	}
	
	function resetSum() {
		var trs = $("#list_div").find("tr:gt(0)");
		var sum = 0;
		$.each(trs, function(i, n){
			//alert($(n).text());
			//if(i==9) sum+=Number($(n).text());
			sum+=Number($(n).find("td:eq(9)").text());
		});
		$("#sum_price").text(sum);
	}
</script>
</head>
<body>
    <div class="main">
        <div class="cont">
            <h2 class="title">回购商品信息：</h2>
            <c:if test="${from eq null }">            
            <div class="ui">
                <div class="label_box">
                	<span class="sel_label fl">商品回购类型：
                		<input type="radio" name="back_type" checked="checked" value="1">退货&nbsp;
						<input type="radio" name="back_type" value="2">回购
                	</span>
                	<a href="${basePath }/buyback/nocode.htm" >查找客人记录</a>
                </div>
                <div class="label_box">
                    <span class="sel_label fl">扫描回购商品：</span>
                    <input type="text" id="code" class="text_input fl">
                </div>
            </div>
            </c:if>
            <h2 class="title"> 查询结果：</h2>
            <div id="list_div">
            	<jsp:include page="list.jsp"></jsp:include>
            	<span class="sel_label fr">商品总金额：<span id="sum_price">0</span>元</span>
            </div>
            
            <div id="customer_div">
            	<jsp:include page="../customer/index.jsp"></jsp:include>
            </div>
            
            <div class="text_c">
	        	<a href="javascript:;" class="yellow_btn btn" onclick="buyconfirm();">确认</a>
	        	&nbsp;&nbsp;
	        	<a href="javascript:;" onclick="clearAll();">全部清空</a>
	        </div>
            
        </div>
    </div>

</body>
</html>